<?php
session_start(); //Do not remove this
    if ($_SESSION['login']!='123!@#'){
		header('location:index.php');
	}
	require "../../php/config.php";
	require "../../php/libs/db_functions.php";
	 $chapter_id =  isset($_POST['chapter_id'])? $_POST['chapter_id'] : 1;
	 $chapter_name =  isset($_POST['chapter_name'])? $_POST['chapter_name'] : 'noname';
	 $chapter_content =  isset($_POST['chapter_content'])? $_POST['chapter_content'] : 'noname';
	 $story_id =  isset($_POST['story_id'])? $_POST['story_id'] : 1;
	//exit();
	  CONNECT_DB();
		mysql_query("SET NAMES utf8");
		$table_name = 'story_id_'.$story_id;
		
		$sql = "CREATE TABLE IF NOT EXISTS ".$table_name." (
				ID int(11) unsigned NOT NULL auto_increment,
				chapter_id int(11) UNIQUE NOT NULL default '1',
				chapter_name varchar(200) NOT NULL default '',
				chapter_content text NOT NULL default '',
				PRIMARY KEY  (ID)
				) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

		mysql_query($sql);
		$sql = "INSERT INTO $table_name (chapter_id, chapter_name, chapter_content) 
				VALUES ($chapter_id,'$chapter_name', '$chapter_content')";
		$result=mysql_query($sql);
		if ($result){
			echo 'successful';
		} else {
			echo 'err - check your chapter id!';
		}
		
		$story_table_name = 'story_id_'.$story_id;
		/**
			update story_chapter_count
		**/
		//UPDATE story_list SET  story_count_chapter = (SELECT MAX(chapter_id) FROM story_id_1) WHERE story_id = 1
		$sql1=	"UPDATE story_list SET  story_count_chapter = (SELECT MAX(chapter_id) FROM $story_table_name) 
				WHERE story_id = $story_id";
		$result1=mysql_query($sql1);
		
		if ($result1){
			//echo 'successful';
		} else {
			//echo 'err - update chapter count fail!';
		}
?>